//
//  CustomTabBarItem.m
//  Meroad
//
//  Created by 郭孝通 on 2019/1/25.
//  Copyright © 2019 郭孝通. All rights reserved.
//

#import "CustomTabBarItem.h"

NSString *const kCustomTabBarItemAttributeTitle = @"CustomTabBarItemAttributeTitle";
NSString *const kCustomTabBarItemAttributeNormalImageName = @"CustomTabBarItemAttributeNormalImageName";
NSString *const kCustomTabBarItemAttributeSelectedImageName = @"CustomTabBarItemAttributeSelectedImageName";
NSString *const kCustomTabBarItemAttributeType = @"CustomTabBarItemAttributeType";

@implementation CustomTabBarItem

-(void)config {
    self.adjustsImageWhenHighlighted = NO;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if(self){
        [self config];
    }
    return self;
}

-(instancetype)init {
    self = [super init];
    
    if(self){
        [self config];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    
    if(self){
        [self config];
    }
    
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    //CustomTabBarItem视图尺寸设置
    //UIButton子视图设置
    //设置titleLabel
    [self.titleLabel sizeToFit];
    CGSize titleSize = self.titleLabel.frame.size;
    
    //设置imageView
    CGSize imageSize = [self imageForState:UIControlStateNormal].size;
    //设置控件imageView中心点(CGPointMake)
    if(imageSize.width != 0 && imageSize.height != 0){
        CGFloat imageViewCenterY = CGRectGetHeight(self.frame) - 3 - titleSize.height - imageSize.height / 2 - 5;
        self.imageView.center = CGPointMake(CGRectGetWidth(self.frame) / 2, IS_IPHONE_XS ? imageViewCenterY + 5 : imageViewCenterY);
    }else{
        CGPoint imageViewCenter = self.imageView.center;
        imageViewCenter.x = CGRectGetWidth(self.frame) / 2;
        imageViewCenter.y = (CGRectGetHeight(self.frame) - titleSize.height) / 2;
        self.imageView.center = imageViewCenter;
    }
    
    CGPoint labelCenter = CGPointMake(CGRectGetWidth(self.frame) / 2 + 0.5, IS_IPHONE_XS ? CGRectGetHeight(self.frame) - titleSize.height / 2 :CGRectGetHeight(self.frame) - 6 - titleSize.height / 2);
    self.titleLabel.center = labelCenter;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
